<template>
    
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-button type="primary" @click="adddialogVisible = true"
        >添加角色</el-button
      >
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="item in scope.row.children" :key="item.id">
              <!-- 一级权限 -->
              <el-col :span="6">
                <el-tag closable effect="dark">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限and三级权限 -->
              <el-col :span="18">
                <el-row v-for="item2 in item.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag closable effect="dark" type="success">{{
                      item2.authName
                    }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      effect="dark"
                      type="warning"
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editFind(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delOpen(scope.row.id)"
              >删除</el-button
            >
            <el-button type="warning" icon="el-icon-setting" size="mini"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加弹框 -->
    <el-dialog title="添加" :visible.sync="adddialogVisible" width="50%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="ruleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹框 -->
    <el-dialog title="编辑" :visible.sync="editdialogVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { roles, addles, fine, editRoles, del } from "../../api/user";
export default {
  name: "",
  data() {
    return {
      tableData: [],
      adddialogVisible: false,
      editdialogVisible: false,
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      editForm: {},
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  computed: {},
  methods: {
    async getList() {
      const { data: res } = await roles();
      //   console.log(res);
      this.tableData = res.data;
    },
    // 添加
    async add() {
      const { data: res } = await addles(this.ruleForm);
      // console.log(res);
      if (res.meta.status == 201) {
        this.$message.success(res.meta.msg);
        this.adddialogVisible = false;
        this.getList();
      } else {
        return this.$message.error(res.meta.msg);
      }
    },
    // 编辑前查询
    async editFind(id) {
      const { data: res } = await fine(id);
      this.editdialogVisible = true;
      // console.log(res);
      this.editForm = res.data;
    },
    //编辑提交
    async edit() {
      const { data: res } = await editRoles(this.editForm.roleId, {
        roleName: this.editForm.roleName,
        roleDesc: this.editForm.roleDesc,
      });
      // console.log(res);
      this.editdialogVisible = false;
      this.getList();
    },
    //删除
    async delOpen(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          const { data: res } = await del(id);
          console.log(res);
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
.el-tag {
  margin-bottom: 7px;
  margin-right: 5px;
}
.el-row {
  border: 1px solid #efefef;
  /* border-collapse: separate; */
  padding: 1px;
  display: flex;
  align-items: center;
}
</style>
